package com.pm.service.official.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.pm.entity.po.official.OfficialBaseData;
import com.pm.entity.po.official.OfficialMeanDevRank;
import com.pm.entity.vo.official.OfficialBaseDataVo;
import com.pm.mapper.official.OfficialBaseDataMapper;
import com.pm.service.impl.BaseServiceImpl;
import com.pm.service.official.OfficialBaseDataService;
import com.pm.service.official.OfficialComRankService;
import com.pm.service.official.OfficialMeanDevRankService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @program: pm_info_api
 * @description:党政一把手基础数据
 * @author: ht
 * @create: 2020-08-26 17:46
 **/
@Service
public class OfficialBaseDataServiceImpl extends BaseServiceImpl<OfficialBaseData> implements OfficialBaseDataService {

    @Autowired
    OfficialBaseDataMapper officialBaseDataMapper;

    @Autowired
    OfficialMeanDevRankService officialMeanDevRankService;

    @Autowired
    OfficialComRankService officialComRankService;

    @Resource
    private void setBaseMapper(OfficialBaseDataMapper officialBaseDataMapper) {
        super.setBaseMapper(officialBaseDataMapper);
    }

    @Override
    public Integer addOne(OfficialBaseData officialBaseData) {
        if (officialBaseData.getId() != null) {
            officialBaseDataMapper.removeOne(officialBaseData.getId());
            return officialBaseDataMapper.addOne(officialBaseData);
        }
        return officialBaseDataMapper.addOne(officialBaseData);
    }

    @Override
    public Integer addBaseData(JSONObject jsonObject) {
        // 组装离均差表数据
        OfficialMeanDevRank officialMeanDevRank = new OfficialMeanDevRank();
        officialMeanDevRank.setUserName(jsonObject.getString("name"));
        officialMeanDevRank.setYear(jsonObject.getString("year"));
        officialMeanDevRank.setTimeNode(jsonObject.getInteger("timeNode"));
        officialMeanDevRankService.addOne(officialMeanDevRank);
        JSONArray jsonArray = jsonObject.getJSONArray("data");

        // 每一项得分数据
        JSONObject arrayData = null;
        OfficialBaseData officialBaseData = null;
        List<OfficialBaseData> officialBaseDataList = new ArrayList<>();
        List<String> keyList = new ArrayList<>();
        for (int i = 0, j = jsonArray.size(); i < j; i++) {
            officialBaseData = new OfficialBaseData();
            arrayData = jsonArray.getJSONObject(i);
            officialBaseData.setMeanDevId(officialMeanDevRank.getId());
            officialBaseData.setTaskId(arrayData.getInteger("taskId"));
            officialBaseData.setScore(arrayData.getInteger("score"));
            officialBaseData.setTimeNode(jsonObject.getInteger("timeNode"));
            officialBaseDataList.add(officialBaseData);
            //officialBaseDataMapper.addOne(officialBaseData);
            // key组成：任务id_年份_上下半年
            String key = new StringBuffer()
                    .append(officialBaseData.getTaskId())
                    .append("_")
                    .append(officialMeanDevRank.getYear())
                    .append("_")
                    .append(officialBaseData.getTimeNode())
                    .toString();
            keyList.add(key);
        }
        officialBaseDataMapper.batchAdd(officialBaseDataList);
        // 设定综合排名和离均差的值
        officialComRankService.batchChange(keyList);
        return 1;
    }

    @Override
    public List<JSONObject> getBaseDataList(OfficialBaseDataVo officialBaseDataVo) {
        List<JSONObject> resultList = new ArrayList<>();
        OfficialMeanDevRank officialMeanDevRank = new OfficialMeanDevRank();
        officialMeanDevRank.setYear(officialBaseDataVo.getYear());
        officialMeanDevRank.setTimeNode(officialBaseDataVo.getTimeNode());
        List<OfficialMeanDevRank> meanDevRankList = officialMeanDevRankService.getList(officialMeanDevRank);
        JSONObject jsonObject = null;
        OfficialMeanDevRank meanDevRank = null;
        for (int i = 0, j = meanDevRankList.size(); i < j; i++) {
            meanDevRank = meanDevRankList.get(i);
            jsonObject = new JSONObject();
            jsonObject.put("userName", meanDevRank.getUserName());
            jsonObject.put("id", meanDevRank.getId());
            officialBaseDataVo.setMeanDevId(meanDevRank.getId());
            List<Map<String, String>> baseDataList = officialBaseDataMapper.getBaseDataList(officialBaseDataVo);
            jsonObject.put("data", baseDataList);
            resultList.add(jsonObject);
        }
        return resultList;
    }

    @Override
    public Integer updateBaseData(JSONObject jsonObject) {
        // 组装离均差表数据
        OfficialMeanDevRank officialMeanDevRank = new OfficialMeanDevRank();
        officialMeanDevRank.setUserName(jsonObject.getString("name"));
        officialMeanDevRank.setYear(jsonObject.getString("year"));
        officialMeanDevRank.setTimeNode(jsonObject.getInteger("timeNode"));
        officialMeanDevRank.setId(jsonObject.getInteger("id"));
        officialMeanDevRankService.update(officialMeanDevRank);

        JSONArray jsonArray = jsonObject.getJSONArray("data");
        // 每一项得分数据
        JSONObject arrayData = null;
        OfficialBaseData officialBaseData = null;
        for (int i = 0, j = jsonArray.size(); i < j; i++) {
            arrayData = jsonArray.getJSONObject(i);
            officialBaseData = new OfficialBaseData();
            officialBaseData.setId(arrayData.getInteger("id"));
            officialBaseData.setTaskId(arrayData.getInteger("taskId"));
            officialBaseData.setScore(arrayData.getInteger("score"));
            officialBaseData.setTimeNode(jsonObject.getInteger("timeNode"));
            officialBaseDataMapper.update(officialBaseData);
        }
        return 1;
    }

    @Override
    public Integer removeByMeanDevId(Integer meanDevId) {
        return officialBaseDataMapper.removeByMeanDevId(meanDevId);
    }

    @Override
    public Double getScoreSumByTaskIdAndTimeNode(String taskId, String timeNode) {
        return officialBaseDataMapper.getScoreSumByTaskIdAndTimeNode(taskId, timeNode);
    }

    @Override
    public List<String> getTaskIdListByMeanDevId(Integer meanDevId) {
        return officialBaseDataMapper.getTaskIdListByMeanDevId(meanDevId);
    }
}
